//
//  ImgAlertAView.m
//  ImgAlertView_A
//
//  Created by 正合适 on 2017/10/19.
//  Copyright © 2017年 正合适. All rights reserved.
//

#import "ImgAlertAView.h"

#define ScreenWidth        [UIScreen mainScreen].bounds.size.width
#define ScreenHeight       [UIScreen mainScreen].bounds.size.height
#define RGBAColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:a]

@interface ImgAlertAView ()
/**图片名称*/
@property (nonatomic, copy) NSString *imgName;
/**标题*/
@property (nonatomic, copy) NSString *titleStr;
/**提示信息*/
@property (nonatomic, copy) NSString *messageStr;
/**容器view*/
@property (nonatomic, strong) UIView *containerView;
/**图片view*/
@property (nonatomic, strong) UIImageView *iconImg;
/**标题label*/
@property (nonatomic, strong) UILabel *titleLabel;
/**提示信息label*/
@property (nonatomic, strong) UILabel *messageLabel;
@end

@implementation ImgAlertAView
#pragma mark - 生命周期
- (instancetype)initWithFrame:(CGRect)frame withImg:(NSString *)img title:(NSString *)title message:(NSString *)message {
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    if (self) {
        _imgName = img?img:@"";
        _titleStr = title?title:@"";
        _messageStr = message?message:@"";
        [self createUI];
    }
    return self;
}

- (instancetype)initWithImg:(NSString *)img title:(NSString *)title message:(NSString *)message {
    return [self initWithFrame:CGRectZero withImg:img title:title message:message];
}

+ (void)alertViewWithTilte:(NSString *)title message:(NSString *)message img:(NSString *)img callback:(void (^)(void))callback {
    ImgAlertAView *alertView = [[ImgAlertAView alloc] initWithImg:img title:title message:message];
    [[UIApplication sharedApplication].keyWindow addSubview:alertView];
    if (callback) {
        alertView.confirmClickBlcok = callback;
    }
    [alertView show];
}

+ (void)alertViewWithTilte:(NSString *)title message:(NSString *)message img:(NSString *)img {
    [ImgAlertAView alertViewWithTilte:title message:message img:img callback:nil];
}

#pragma mark - 初始化界面
- (void)createUI {
    self.backgroundColor = RGBAColor(0, 0, 0, 0.4);
}

#pragma mark - 布局views
- (void)layoutViews {
    float cWidth = ScreenWidth*0.76;
    float iconWidth = cWidth*0.40;
    float iconheight = iconWidth*1.03;
    float marginTop = _imgName.length>0?iconheight*0.4:iconheight;
    
    _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cWidth, cWidth)];
    _containerView.backgroundColor = RGBAColor(0, 0, 0, 0);
    _containerView.clipsToBounds = YES;
    [self addSubview:_containerView];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, marginTop, _containerView.frame.size.width, _containerView.frame.size.height)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 8;
    [_containerView addSubview:bgView];
    
    _iconImg = [[UIImageView alloc] initWithFrame:CGRectMake((cWidth-iconWidth)/2.0, -marginTop, iconWidth, iconheight)];
    _iconImg.image = [UIImage imageNamed:_imgName];
    [bgView addSubview:_iconImg];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _iconImg.frame.size.height+_iconImg.frame.origin.y+30, cWidth, 22)];
    _titleLabel.font = [UIFont systemFontOfSize:22];
    _titleLabel.textColor = RGBAColor(51, 51, 51, 1);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.text = _titleStr;
    [bgView addSubview:_titleLabel];
    
    _messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _titleLabel.frame.size.height+_titleLabel.frame.origin.y+10, cWidth, 20)];
    _messageLabel.textColor = RGBAColor(112, 112, 112, 1);
    _messageLabel.font = [UIFont systemFontOfSize:15];
    _messageLabel.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:_messageLabel];

    NSString *textStr = _messageStr?_messageStr:@"";
    // 调整行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textStr];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [textStr length])];
    _messageLabel.attributedText = attributedString;
    _messageLabel.numberOfLines = 0;
    [_messageLabel sizeToFit];
    _messageLabel.frame = CGRectMake((cWidth-_messageLabel.frame.size.width)/2.0, _messageLabel.frame.origin.y, _messageLabel.frame.size.width, _messageLabel.frame.size.height);
    
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, _messageLabel.frame.size.height+_messageLabel.frame.origin.y+25, cWidth, 20)];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:19];
    [confirmBtn setTitle:@"我知道了" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:RGBAColor(73, 144, 226, 1) forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:confirmBtn];
    
    float nWidth = cWidth;
    float nHeight = confirmBtn.frame.size.height+confirmBtn.frame.origin.y+25;
    float containerHeight = nHeight + marginTop;
    bgView.frame = CGRectMake(0, marginTop, nWidth, nHeight);
    _containerView.frame = CGRectMake((ScreenWidth-nWidth)/2.0, (ScreenHeight-containerHeight)/2.0-marginTop, nWidth, containerHeight);
    [self animation:_containerView];
}

#pragma mark - 展开动画
- (void)animation:(UIView *)view {
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.55;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.04, 1.04, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.96, 0.96, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [view.layer addAnimation:animation forKey:nil];
}

#pragma mark - 移除
- (void)close {
    [self removeFromSuperview];
}

#pragma mark - 显示
- (void)show {
    [self layoutViews];
}


@end
